﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Configuration;
using DBSync.Util;
using DbSync;

namespace DBSync.wpfPage
{
    /// <summary>
    /// Interaction logic for SettingsPage.xaml
    /// </summary>
    public partial class SettingsPage : Page
    {
        private static SettingsPage instance;
        private static SyncConnection syncConnection = SyncConnection.Instance;
        public static bool validPHRDb = false;
        public static bool validDCDRDb = false;
        public SettingsPage()
        {
            InitializeComponent();
        }

        public static SettingsPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SettingsPage();
                }

                return instance;
            }
        }

        private void btnDCDR_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Documents";
            dlg.DefaultExt = ".sdf";
            dlg.Filter = "SDF File (.sdf)|*.sdf";

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                txtDCDRDBPath.Text = dlg.FileName;
            }
        }

        private void btnPHRDB_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Documents";
            dlg.DefaultExt = ".sdf";
            dlg.Filter = "SDF File (.sdf)|*.sdf";

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                txtPHRDBPath.Text = dlg.FileName;
            }
        }

        private void btnSettingsSave_Click(object sender, RoutedEventArgs e)
        {
            if (!validPHRDb || !validDCDRDb)
            {
                MSGWindow msg = new MSGWindow(Messages.TTL_DBINVALID, Messages.MSG_INVALID_DB);
                msg.ShowDialog();                
            }
            else
            {
                syncConnection.InitialSet = true;
                syncConnection.Host = txtServerHost.Text;
                syncConnection.Port = Convert.ToInt32(txtServerPort.Text);
                syncConnection.Username = txtServerUsername.Text;
                syncConnection.Password = txtServerPassword.Password;
                syncConnection.PHRDbPath = txtPHRDBPath.Text;
                syncConnection.DCDRDbPath = txtDCDRDBPath.Text;

                if (SessionData.user.USERNAME == "axiohelix")
                {
                    SyncKeys.PHRDBNAME = SyncKeys.PHRDBNAME_DEFAULT;
                    SyncKeys.DCDRDBNAME = SyncKeys.DCDRDBNAME_DEFAULT;
                }
                else
                {
                    SyncKeys.PHRDBNAME = SyncKeys.PHRDBNAME_DEFAULT + "_" + SessionData.organization.ID;
                    SyncKeys.DCDRDBNAME = SyncKeys.DCDRDBNAME_DEFAULT + "_" + SessionData.organization.ID;
                }
                SyncConnection.updateAppSettings();
                SyncConnection.updateConnectionStrings();
                HomePage.Instance.resetProgress();
                MainWindow.Instance.ContentFrame.Content = HomePage.Instance;
            }
        }

        private void btnSettingsReset_Click(object sender, RoutedEventArgs e)
        {
            loadAppSettings();
            loadDataPage();
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            loadAppSettings();
            loadDataPage();
            if (SessionData.user.ORGANIZATION_ID != null)
            {
                txtOrganizationId.Text = SessionData.user.ORGANIZATION_ID.ToString();
            }
        }

        private void loadDataPage()
        {
            txtServerHost.Text = syncConnection.Host;
            txtServerPort.Text = syncConnection.Port.ToString();
            txtServerUsername.Text = syncConnection.Username;
            txtServerPassword.Password = syncConnection.Password;
            txtPHRDBPath.Text = syncConnection.PHRDbPath;
            txtDCDRDBPath.Text = syncConnection.DCDRDbPath;
            validDCDRDb = syncConnection.ValidDCDRDb;
            validPHRDb = syncConnection.ValidPHRDb;
        }

        private void loadAppSettings()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = config.AppSettings.Settings;
            //syncConnection.InitialSet = Convert.ToBoolean(settings[SyncKeys.INITIALSET].Value);
            syncConnection.Host = settings[SyncKeys.HOST].Value;
            syncConnection.Port = Convert.ToInt32(settings[SyncKeys.PORT].Value);
            syncConnection.Username = settings[SyncKeys.USERNAME].Value;
            syncConnection.Password = settings[SyncKeys.PASSWORD].Value;
            syncConnection.PHRDbPath = settings[SyncKeys.PHRDBPATH].Value;
            syncConnection.DCDRDbPath = settings[SyncKeys.DCDRDBPATH].Value;
            syncConnection.ValidPHRDb = Convert.ToBoolean(settings[SyncKeys.VALIDPHRDB].Value);
            syncConnection.ValidDCDRDb = Convert.ToBoolean(settings[SyncKeys.VALIDDCDRDB].Value);
        }

        private void txtPHRDBPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            string dbPath = txtPHRDBPath.Text;
            string dbName = "personalhr.sdf";
            validPHRDb = validateDatabaseSelection(dbPath,dbName);
            if (validPHRDb)
            {
                syncConnection.ValidPHRDb = true;
            }
        }

        private bool validateDatabaseSelection(string dbPath, string dbName)
        {
            int dbNameLength = dbName.Length;
            int pathLength = dbPath.Length;
            if (pathLength >= dbNameLength)
            {
                string db = dbPath.Substring(pathLength - dbNameLength, dbNameLength);
                if (db != dbName)
                {
                    MSGWindow msg = new MSGWindow(Messages.TTL_DBINVALID, "Please select " + dbName + " database");
                    msg.ShowDialog();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        private void txtDCDRDBPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            string dbPath = txtDCDRDBPath.Text;
            string dbName = "drcardrive.sdf";
            validDCDRDb = validateDatabaseSelection(dbPath, dbName);
            if (validDCDRDb)
            {
                syncConnection.ValidDCDRDb = true;
            }
        }


    }
}
